#include "Preferences.h"
#include <string.h>
#include <malloc.h>
#include <direct.h>
Preferences* LoadPreference(char *path){

	FILE* file = NULL;
	file = fopen(path, "r");
	if (file == NULL){ 
		printf("Failed Loading config\n");
		return NULL; }
	printf("\n");
	char tmpLine[200] = { 0 };
	int nitem = 0;
	Preferences *config = new Preferences();//(Preferences*)malloc(sizeof(Preferences));
	//config->Preferences();
	while ((nitem = fscanf(file, "%[^=\n]%*c", tmpLine)) != EOF){
		int i = 0;
		if (tmpLine[0] == '#')
		{
			//fscanf(file, "%*[^\n]%*c");
			continue;
		}
		else if (i < strlen(tmpLine)){
			//Comment Config
			if (strstr(tmpLine, "Window_Type") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Window_Type);
				printf("Window_Type = %d \n", config->Window_Type);
			}
			else
			if (strstr(tmpLine, "MaxFreq_Separate") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->MaxFreq_Separate);
				printf("MaxFreq_Separate = %d \n", config->MaxFreq_Separate);
			}
			else
			if (strstr(tmpLine, "Overlap_Separate") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Overlap_Separate);
				printf("Overlap_Separate = %d \n", config->Overlap_Separate);
			}
			else
			if (strstr(tmpLine, "BlockSize_Separate") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->BlockSize_Separate);
				printf("BlockSize_Separate = %d \n", config->BlockSize_Separate);
			}
			else
			if (strstr(tmpLine, "BandFrequency_Separate") != 0){
				int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%f", &config->BandFrequency_Separate);
				printf("BandFrequency_Separate = %d \n", config->BandFrequency_Separate);
			}
			//Channel Option for Separate
			else
			if (strstr(tmpLine, "Noise_Channel") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Noise_Channel);
				printf("Noise_Channel = %d \n", config->Noise_Channel);
			}
			else if (strstr(tmpLine, "Fundamental") != 0){ //NEWCODE
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Fundamental);
				printf("Fundamental = %d \n", config->Fundamental);
			}

			else if (strstr(tmpLine, "Original") != 0){ //NEWCODE
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Original);
				printf("Original = %d \n", config->Original);
			}
			else if (strstr(tmpLine, "Recreate_Harmonics") != 0){ //NEWCODE
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Recreate_Harmonics);
				printf("Recreate_Harmonics = %d \n", config->Recreate_Harmonics);
			}
			else if (strstr(tmpLine, "Combined") != 0){ //NEWCODE
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Combined);
				printf("Combined = %d \n", config->Combined);
			}
			else if (strstr(tmpLine, "Individual_Harmonics") != 0){ //NEWCODE
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Individual_Harmonics);
				printf("Individual_Harmonics = %d \n", config->Individual_Harmonics);
			}
			else if (strstr(tmpLine, "Harmonics") != 0){ //NEWCODE
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Harmonics);
				printf("Harmonics = %d \n", config->Harmonics);
			}
			// End channel option
			else
			if (strstr(tmpLine, "TestChannel") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->TestChannel);
				printf("TestChannel = %d \n", config->TestChannel);
			}
			else
			if (strstr(tmpLine, "AmplitudeGraph") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->AmplitudeGraph);
				printf("AmplitudeGraph = %d \n", config->AmplitudeGraph);
			}
			/*else if (strstr(tmpLine, "Resynthesize") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Resynthesize);
				printf("Resynthesize = %d \n", config->Resynthesize);
			}*/
			else if (strstr(tmpLine, "Normalize_for_Detection") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Normalize_for_Detection);
				printf("Normalize_for_Detection = %d \n", config->Normalize_for_Detection);
			}
			else if (strstr(tmpLine, "Helmholtz_PositivePeak") != 0){
				//int d = 0;
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->PositivePeak);
				printf("Helmholtz_PositivePeak = %d \n", config->PositivePeak);
			}
			else if (strstr(tmpLine, "Helmholtz_NegativePeak") != 0){
					//int d = 0;
					fscanf(file, "%[^\n#]%*c", tmpLine);
					sscanf(tmpLine, "%d", &config->NegativePeak);
					printf("Helmholtz_NegativePeak = %d \n", config->NegativePeak);
			}
			//else if (strstr(tmpLine, "RenameFiles") != 0){
			//		//int d = 0;
			//		fscanf(file, "%[^\n#]%*c", tmpLine);
			//		sscanf(tmpLine, "%d", &config->RenameFiles);
			//		printf("RenameFiles = %d \n", config->RenameFiles);
			//}
			else if (strstr(tmpLine, "OutputFolder") != 0){
					char tmpPath[200] = { 0 };
					fscanf(file, "%[^\n#]%*c", tmpLine);
					int k = strlen(tmpLine) - 1;
					while (tmpLine[k] == ' ' || tmpLine[k] == '\"')
					{
						tmpLine[k] = '\0';
						k--;
					}
					int j = 0;
					while (tmpLine[j] == ' ' || tmpLine[j] == '\"')
					{
						j++;
					}
					
					sscanf(tmpLine + j, "%[^\n#]", &tmpPath);
					strcpy(config->OutputFolder, tmpPath);
					printf("OutputFolder = \"%s\" \n", config->OutputFolder);
					config->isValidateOutputFolder();
			}
			else if (strstr(tmpLine, "AnalysisChannel") != 0){
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Channel);
				if (config->Channel > 0){
					printf("AnalysisChannel = %d \n", config->Channel);
					config->Channel = config->Channel - 1;
				}		
			}
			else if (strstr(tmpLine, "Blocksize") != 0){
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->blockSize);
				if (config->blockSize > 0){
					//int tmp = pow(2.0, (int)(log((float)config->blockSize) / (float)log(2.0)));
					//if (config->blockSize != //hutngn pno need for this, we do not need limitation on blockSize)
					printf("BlockSize = %d \n", config->blockSize);
				}
			}
			//Helmholtz
			else if (strstr(tmpLine, "Helmholtz_Graph") != 0){
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->pitchGraph_Hel);
				printf("Helmholtz_Graph = %d \n", config->pitchGraph_Hel);
			}
			else if (strstr(tmpLine, "Helmholtz_Sine") != 0){
					fscanf(file, "%[^\n#]%*c", tmpLine);
					sscanf(tmpLine, "%d", &config->Helmholtz_Sinewave);
					printf("Helmholtz_Sine = %d \n", config->Helmholtz_Sinewave);
			}
			else if (strstr(tmpLine, "Helmholtz_Fidelity") != 0){ 
					fscanf(file, "%[^\n#]%*c", tmpLine);
					sscanf(tmpLine, "%f", &config->Helmholtz_Fidelity);
					printf("Helmholtz_Fidelity = %2.3f \n", config->Helmholtz_Fidelity);
			}
			// Sigmund Config
			else if (strstr(tmpLine, "Sigmund_Graph") != 0){
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->pitchGraph_Sig);
				printf("Sigmund_Graph = %d \n", config->pitchGraph_Sig);
			}
			else if (strstr(tmpLine, "Sigmund_Npts") != 0){
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Sigmund_Npts);
				printf("Sigmund_Npts = %d \n", config->Sigmund_Npts);
			}
			else if (strstr(tmpLine, "Sigmund_HopSize") != 0){
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Sigmund_HopSize);
				printf("Sigmund_HopSize = %d \n", config->Sigmund_HopSize);
			}
			else if (strstr(tmpLine, "Sigmund_Sine") != 0){
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->Sigmund_Sinewave);
				printf("Sigmund_Sine = %d \n", config->Sigmund_Sinewave);
			}
			else if (strstr(tmpLine, "DelayCompensation_Sig") != 0){
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%ud", &config->DelayCompensation_Sig);
				printf("DelayCompensation_Sig = %d \n", config->DelayCompensation_Sig);
			}
			else if (strstr(tmpLine, "RenameFiles_AverageFrequency") != 0){ 
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->RenameFiles_AverageFrequency);
				printf("RenameFiles_AverageFrequency = %d \n", config->RenameFiles_AverageFrequency);
			}
			else if (strstr(tmpLine, "RenameFiles_NoteName") != 0){ 
				fscanf(file, "%[^\n#]%*c", tmpLine);
				sscanf(tmpLine, "%d", &config->RenameFiles_NoteName);
				printf("RenameFiles_NoteName = %d \n", config->RenameFiles_NoteName);
			}
			else if(strstr(tmpLine, "RenameFiles_Prefix") != 0){
				char tmpPath[200] = { 0 };
				fscanf(file, "%[^\n#]%*c", tmpLine);
				int k = strlen(tmpLine) - 1;
				while (tmpLine[k] == ' ' || tmpLine[k] == '\"')
				{
					tmpLine[k] = '\0';
					k--;
				}
				int j = 0;
				while (tmpLine[j] == ' ' || tmpLine[j] == '\"')
				{
					j++;
				}
				
				sscanf(tmpLine + j, "%[^\n#]", &tmpPath);
				strcpy(config->RenameFiles_Prefix, tmpPath);
				printf("RenameFiles_Prefix = \"%s\" \n", config->RenameFiles_Prefix);
			}
			
			else {
				//fscanf(file, "%*[^\n]%*c", tmpLine);
				continue;
			}
		}
		tmpLine[0] = '#';
	}
	return config;
}

